package org.jeecg.modules.demo.ch.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.ch.Utils.DateUtil;
import org.jeecg.modules.demo.ch.common.CommonMst;
import org.jeecg.modules.demo.ch.entity.ChWorkPlanForNextWeek;
import org.jeecg.modules.demo.ch.service.IChWorkPlanForNextWeekService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;

 /**
 * @Description: 下周计划表
 * @Author: jeecg-boot
 * @Date:   2022-11-29
 * @Version: V1.0
 */
@Api(tags="下周计划表")
@RestController
@RequestMapping("/ch/chWorkPlanForNextWeek")
@Slf4j
public class ChWorkPlanForNextWeekController extends JeecgController<ChWorkPlanForNextWeek, IChWorkPlanForNextWeekService> {
	@Autowired
	private IChWorkPlanForNextWeekService chWorkPlanForNextWeekService;

	 /**
	  * 生成下周计划表标题
	  *
	  * @return
	  */
	 public  String generateWeeklyTitle() {
		 //获取下周一的日期
		 SimpleDateFormat sf=new SimpleDateFormat(CommonMst.WEEKLYREPORT.DATEFORMAT);
		 String nextMonday = sf.format(DateUtil.getNextWeek().get("monday"));//下周一
		 LocalDate localDate = LocalDate.parse(nextMonday, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
		 String date = localDate.toString();
		 int weekByWed = 0;//获取第几周
		 try {
			 weekByWed = DateUtil.getWeekByWed(date, CommonMst.WEEKLYREPORT.DATEFORMAT);
		 } catch (Exception e) {
			 log.error("下周计划表-生成下周计划表标题异常", e);
			 e.printStackTrace();
		 }
		 if (weekByWed > 4){
			 //大于4 月份减一
			 localDate = localDate.minusMonths(1);
		 }
		 int  year = localDate.getYear();//获取年
		 int month = localDate.getMonthValue();//获取月

		 StringBuilder sb = new StringBuilder();
		 String title = sb.append(year).append("年").append(month).append("月第").append(weekByWed).append("周").toString();
		 log.info("当前下周一的日期:{},属于当月的第{}周,生成的标题title:{}", date, weekByWed, title);
		 return title;
	 }


	/**
	 * 分页列表查询
	 *
	 * @param chWorkPlanForNextWeek
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "下周计划表-分页列表查询")
	@ApiOperation(value="下周计划表-分页列表查询", notes="下周计划表-分页列表查询")
//	@GetMapping(value = "/list")
	// 需要将前端将list请求地址改成此地址
	@GetMapping(value = "/mpList")
	@PermissionData(pageComponent = "weekReport/ChWorkPlanForNextWeekList")
	public Result<IPage<ChWorkPlanForNextWeek>> queryPageList(ChWorkPlanForNextWeek chWorkPlanForNextWeek,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
//		QueryWrapper<ChWorkPlanForNextWeek> queryWrapper = QueryGenerator.initQueryWrapper(chWorkPlanForNextWeek, req.getParameterMap());
		String title = chWorkPlanForNextWeek.getTitle();//标题
		String name = chWorkPlanForNextWeek.getName();//姓名
		String keyWorkPlan = chWorkPlanForNextWeek.getKeyWorkPlan();//下周重点工作计划
		String keyPoints = chWorkPlanForNextWeek.getKeyPoints();//关键点
		//加载数据权限
		//此处使用的是QueryWrapper，如果你需要用LambdaQueryWrapper，请先new一个QueryWrapper，接着加载数据权限，然后通过queryWrapper.lambda()将其转化成LambdaQueryWrapper，最后你就能使用LambdaQueryWrapper处理自己的逻辑了
		QueryWrapper<ChWorkPlanForNextWeek> queryWrapper = new QueryWrapper<>();
		//调用这个QueryGenerator.installAuthMplus方法加载数据权限
		QueryGenerator.installAuthMplus(queryWrapper, ChWorkPlanForNextWeek.class);
		//开始自己的逻辑
		LambdaQueryWrapper<ChWorkPlanForNextWeek> chWorkPlanForNextWeekLambdaQueryWrapper = queryWrapper.lambda()
				.like(StringUtils.hasLength(title), ChWorkPlanForNextWeek::getTitle, title)
				.like(StringUtils.hasLength(name), ChWorkPlanForNextWeek::getName, name)
				.like(StringUtils.hasLength(keyWorkPlan), ChWorkPlanForNextWeek::getKeyWorkPlan, keyWorkPlan)
				.like(StringUtils.hasLength(keyPoints), ChWorkPlanForNextWeek::getKeyPoints, keyPoints)
				.orderByDesc(ChWorkPlanForNextWeek::getName)
				.orderByDesc(ChWorkPlanForNextWeek::getCreateTime);
		Page<ChWorkPlanForNextWeek> page = new Page<ChWorkPlanForNextWeek>(pageNo, pageSize);
		IPage<ChWorkPlanForNextWeek> pageList = chWorkPlanForNextWeekService.page(page, chWorkPlanForNextWeekLambdaQueryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param chWorkPlanForNextWeek
	 * @return
	 */
	@AutoLog(value = "下周计划表-添加")
	@ApiOperation(value="下周计划表-添加", notes="下周计划表-添加")
	//@RequiresPermissions("org.jeecg.modules.demo:ch_work_plan_for_next_week:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody ChWorkPlanForNextWeek chWorkPlanForNextWeek) {
		chWorkPlanForNextWeek.setTitle(generateWeeklyTitle());//生成标题
		chWorkPlanForNextWeekService.save(chWorkPlanForNextWeek);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param chWorkPlanForNextWeek
	 * @return
	 */
	@AutoLog(value = "下周计划表-编辑")
	@ApiOperation(value="下周计划表-编辑", notes="下周计划表-编辑")
	//@RequiresPermissions("org.jeecg.modules.demo:ch_work_plan_for_next_week:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody ChWorkPlanForNextWeek chWorkPlanForNextWeek) {
		chWorkPlanForNextWeekService.updateById(chWorkPlanForNextWeek);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "下周计划表-通过id删除")
	@ApiOperation(value="下周计划表-通过id删除", notes="下周计划表-通过id删除")
	//@RequiresPermissions("org.jeecg.modules.demo:ch_work_plan_for_next_week:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		chWorkPlanForNextWeekService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "下周计划表-批量删除")
	@ApiOperation(value="下周计划表-批量删除", notes="下周计划表-批量删除")
	//@RequiresPermissions("org.jeecg.modules.demo:ch_work_plan_for_next_week:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.chWorkPlanForNextWeekService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "下周计划表-通过id查询")
	@ApiOperation(value="下周计划表-通过id查询", notes="下周计划表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<ChWorkPlanForNextWeek> queryById(@RequestParam(name="id",required=true) String id) {
		ChWorkPlanForNextWeek chWorkPlanForNextWeek = chWorkPlanForNextWeekService.getById(id);
		if(chWorkPlanForNextWeek==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(chWorkPlanForNextWeek);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param chWorkPlanForNextWeek
    */
    //@RequiresPermissions("org.jeecg.modules.demo:ch_work_plan_for_next_week:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ChWorkPlanForNextWeek chWorkPlanForNextWeek) {
        return super.exportXls(request, chWorkPlanForNextWeek, ChWorkPlanForNextWeek.class, "下周计划表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("ch_work_plan_for_next_week:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ChWorkPlanForNextWeek.class);
    }

}
